package com.company.cloud.company.modules.app.service.impl;

import com.company.cloud.captcha.modules.util.StringUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.common.utils.LocalDateTimeUtils;
import com.company.cloud.company.constant.CompanyConstant;
import com.company.cloud.company.constant.ErrorCompanyEnum;
import com.company.cloud.company.modules.app.service.CompanyAppService;
import com.company.cloud.company.modules.base.entity.CompanyInfo;
import com.company.cloud.company.modules.base.service.CompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CompanyAppServiceImpl implements CompanyAppService {


    @Autowired
    private CompanyInfoService companyInfoService;

    @Override
    public CompanyInfo checkCompanyermission(String companyNo, Integer userId) {
        if(CheckUtils.isEmpty(userId)){
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        CompanyInfo entity=this.checkCompanyermission(companyNo);
        if(entity.getAdminUserId()!=userId){
            throw new BusinessException(BaseReturnEnum.NOT_AUTHC);
        }


        return entity;
    }

    @Override
    public CompanyInfo checkCompanyermission(String companyNo) {
        if(StringUtils.isEmpty(companyNo)){
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        CompanyInfo entity= companyInfoService.getById(companyNo);
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        if(!CompanyConstant.CompanyState.CS_Open.getCode().equals(entity.getState())){
            throw new BusinessException(ErrorCompanyEnum.Company_Not_Use);
        }
        //todo:判断使用期限
//        LocalDateTimeUtils.compare()
//        LocalDateTime nowDate= LocalDateTime.now();
        return entity;
    }
}
